package main;


import lombok.AllArgsConstructor;
import lombok.Data;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 银行类
 */
class Bank {
    private List<Account> accounts;

    public Bank(int accountNumber, String accountName, double initMoney) {
        this.accounts = new ArrayList<>();
        for (int i = 1; i <= accountNumber; i++) {
            this.accounts.add(new Account(initMoney, accountName + i));
        }
    }

    /**
     * 转账
     *
     * @param from  资金来源
     * @param to    资金流向
     * @param money 金额
     */
    public synchronized void transfer(int from, int to, double money) {
        try {
            //获取账户对象
            Account fromAccount = this.accounts.get(from);
            if (fromAccount.getDeposit() < money) {
                System.out.println("账户" + (from + 1) + "余额不足");
                //使该线程进入等待状态
                wait();
                return;
            }
            Account toAccount = this.accounts.get(to);
            fromAccount.setDeposit(fromAccount.getDeposit() - money);
            toAccount.setDeposit(toAccount.getDeposit() + money);
            System.out.println("------------------------------------------------");
            System.out.println(Thread.currentThread().getName() + ":" + fromAccount.getName() + "向" + toAccount.getName() + "转账" + money + "￥");
            DecimalFormat df = new DecimalFormat(".00");
            System.out.println("当前银行总金额为:" + df.format(getTotalMoney()));
            System.out.println("------------------------------------------------");
            //唤醒所有等待的线程
            notifyAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取银行当前总金额
     *
     * @return
     */
    public double getTotalMoney() {
        double sum = 0;
        for (Account account : this.accounts) {
            sum += account.getDeposit();
        }
        return sum;
    }

    public int getSize() {
        return this.accounts.size();
    }
}

/**
 * 账户类
 */
@Data
@AllArgsConstructor
class Account {
    /**
     * 账户存款
     */
    private double deposit;
    /**
     * 账户名称
     */
    private String name;

}

/**
 * User: R
 * Date: 2018/8/27
 * Time: 10:02
 * Created with IntelliJ IDEA.
 */
public class ThreadDemo {
    private static final int ACCOUNT_NUMBER = 100;
    private static final String ACCOUNT_NAME = "账户";
    private static final double INIT_MONEY = 100000;
    private static final double TRANSFER_MONEY = 10000;

    public static void main(String[] args) {
        int[][] num = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int[] num1 = new int[]{1, 2, 3};
        System.out.println(Arrays.deepToString(num));
        //初始化银行以及账户对象
//        Bank bank = new Bank(ACCOUNT_NUMBER, ACCOUNT_NAME, INIT_MONEY);
//        for (int i = 0; i < bank.getSize(); i++) {
//            int from = i;
//            Runnable runnable = () -> {
//                while (true) {
//                    try {
//                        int to = (int) (ACCOUNT_NUMBER * Math.random());
//                        double money = TRANSFER_MONEY * Math.random();
//                        bank.transfer(from, to, money);
//                        Thread.sleep((long) (1000 * Math.random()));
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            };
//            Thread thread = new Thread(runnable);
//            thread.start();
//        }
    }
}

